package net.mysoft.framework.util;

import net.mysoft.framework.cache.Cache;
import net.mysoft.framework.cache.CacheManager;

import org.apache.log4j.Logger;

public class FormulaOperation {

	private static final Logger logger = Logger.getLogger(FileUtil.class);

	public static String getFormulaByName(String formulaByCode){
		int startIndex = formulaByCode.indexOf("@");
		if(startIndex >= 0 ){
			int endIndex = formulaByCode.indexOf(")",startIndex);
			String parameter = formulaByCode.substring(startIndex, endIndex + 1);
			int cycle = Integer.parseInt(parameter.substring(parameter.indexOf("(") + 1, parameter.indexOf(")")));
			String indication = parameter.substring(parameter.indexOf("@") + 1, parameter.indexOf("("));
			Cache cac = CacheManager.getContent(indication);
			if(cac != null && !"".equals(cac)){
				/*QiYeCWZB qiYeCWZB = (QiYeCWZB)cac.getValue();
				formulaByCode = formulaByCode.replace(parameter, qiYeCWZB.getMingCheng());*/
			}
			logger.debug("模型指标公式：" + formulaByCode);
			return getFormulaByName(formulaByCode);
		}else{
			logger.debug("模型指标公式：" + formulaByCode);
			return formulaByCode;
		}
	}

	public static String getFormulaByNameTest(String formulaByCode){
		int startIndex = formulaByCode.indexOf("@");
		if(startIndex >= 0 ){
			int endIndex = formulaByCode.indexOf(")",startIndex);
			String parameter = formulaByCode.substring(startIndex, endIndex + 1);
			int cycle = Integer.parseInt(parameter.substring(parameter.indexOf("(") + 1, parameter.indexOf(")")));
			String indication = parameter.substring(parameter.indexOf("@") + 1, parameter.indexOf("("));
			formulaByCode = formulaByCode.replace(parameter, indication);
			logger.debug("模型指标公式：" + formulaByCode);
			return getFormulaByNameTest(formulaByCode);
		}else{
			logger.debug("模型指标公式：" + formulaByCode);
			return formulaByCode;
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String formual = "@50010100681(0)/(@50010400001(0)+@50010400011(0)+@50010400021(0)+@50010400031(0)+@50010400041(0))";
		FormulaOperation fo = new FormulaOperation();
		fo.getFormulaByNameTest(formual);
	}

}
